﻿// -----------------------------------------------------------------------
// <copyright file="TextParser.cs" company="EPAM Systems">
////Copyright (c) EPAM Systems, 2012. All rights reserved.
// </copyright>
// -----------------------------------------------------------------------

namespace MBOP.Helpers.Parsers
{
	#region Usings

	using System.Text;
	using System.Text.RegularExpressions;

	#endregion

	/// <summary>
	/// Provides parsing operations for text.
	/// </summary>
	public static class TextParser
	{
		#region Constants

		/// <summary>
		/// Represents the special tags reg exp pattern.
		/// </summary>
		private const string SPEC_TAG_PATTERN = @"<[ ]*(p|br[ /]*|li)[\w\""=: ]*>";

		/// <summary>
		/// Represents theh header tag reg exp pattern.
		/// </summary>
		private const string HEADER_TAG_PATTERN = "<[ ]*[/]*[ ]*h[0-9]*[ ]*>";

		/// <summary>
		/// Represents the ignore reg exp pattern.
		/// </summary>
		private const string IGNORE_PATTERN = @"<[^>]+>|[ ]+(?=\r\n)|(?<=\r\n)[ ]+";

		/// <summary>
		/// Represents the space sequene reg exp pattern.
		/// </summary>
		private const string SPACE_SEQUENCE_PATTERN = "[ ]+";

		/// <summary>
		/// Represents the space symbol.
		/// </summary>
		private const char SPACE_SYMBOL = ' ';

		/// <summary>
		/// Represents the new line symbol.
		/// </summary>
		private const char NEW_LINE_SYMBOL = '\n';

		/// <summary>
		/// Represents the tab symbol.
		/// </summary>
		private const char TAB_SYMBOL = '\t';

		/// <summary>
		/// Represents the carriage return symbol.
		/// </summary>
		private const char CARRIAGE_RETURN_SYMBOL = '\r';

		#endregion

		#region Public Methods

		/// <summary>
		/// Parses html to plain text without formatting.
		/// </summary>
		/// <param name="htmlSource">The html source</param>
		/// <returns>The plain text</returns>
		public static string ParseFromHtml(string htmlSource)
		{
			Regex specTagReplaceRegEx = new Regex(SPEC_TAG_PATTERN, RegexOptions.IgnoreCase);

			StringBuilder formattedSequenceBuilder = new StringBuilder();

			formattedSequenceBuilder.Append(CARRIAGE_RETURN_SYMBOL);
			formattedSequenceBuilder.Append(NEW_LINE_SYMBOL);

			string replacedString = specTagReplaceRegEx.Replace(htmlSource, formattedSequenceBuilder.ToString());

			Regex headerReplaceRegEx = new Regex(HEADER_TAG_PATTERN);

			formattedSequenceBuilder.Append(CARRIAGE_RETURN_SYMBOL);
			formattedSequenceBuilder.Append(NEW_LINE_SYMBOL);

			replacedString = headerReplaceRegEx.Replace(replacedString, formattedSequenceBuilder.ToString());

			Regex removeTagRegEx = new Regex(IGNORE_PATTERN, RegexOptions.IgnoreCase);

			replacedString = removeTagRegEx.Replace(replacedString, string.Empty);

			Regex spaceSeqRegEx = new Regex(SPACE_SEQUENCE_PATTERN, RegexOptions.IgnoreCase);

			replacedString = spaceSeqRegEx.Replace(replacedString, SPACE_SYMBOL.ToString());

			return replacedString.Trim(SPACE_SYMBOL, CARRIAGE_RETURN_SYMBOL, NEW_LINE_SYMBOL, TAB_SYMBOL);
		}

		#endregion
	}
}